package com.lili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lili.base.context.WebContext;
import com.lili.base.vo.ResultAPI;
import com.lili.config.cache.RedisCache;
import com.lili.mapper.*;
import com.lili.pojo.*;
import com.lili.service.*;
import com.lili.vo.request.admin.*;
import com.lili.vo.request.other.PageVO;
import com.lili.vo.response.admin.MessageRespVO;
import com.lili.vo.response.troop.TroopMessageRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
        implements MessageService {

    private MessageUserMapper messageUserMapper;
    private UserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserService userService;

    @Autowired
    public MessageServiceImpl(MessageUserMapper messageUserMapper, UserMapper userMapper) {
        this.messageUserMapper = messageUserMapper;
        this.userMapper = userMapper;
    }

    @Override
    public IPage<MessageRespVO> getPageList(MessageReqVO vo) {
        IPage<MessageRespVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        IPage<MessageRespVO> pages = baseMapper.getByPage(page, vo);
        return pages;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendMessage(MessageSendReqVO vo, Integer createUser) {
        List<Integer> userListId = sendUserList(vo);
        Message message = new Message();
        BeanUtils.copyProperties(vo, message);
        message.setReceiveNum(userListId.size());
        message.setActualReceiveNum(0);
        message.setCreateUser(createUser);
        baseMapper.insert(message);
        Integer messageId = message.getId();
        userListId.stream().forEach(id -> {
            MessageUser messageUser = new MessageUser();
            messageUser.setReceiveId(id);
            messageUser.setReaded(0);
            messageUser.setCreateUser(createUser);
            messageUser.setMessageId(messageId);
            messageUserMapper.insert(messageUser);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultAPI<String> readMessage(Integer id) {
        //查看这条信息是否被阅读过
        User currentUser = new WebContext(redisCache, userService).getCurrentUser();
        MessageUser messageUser = messageUserMapper.selectByInfo(currentUser.getId(), id);
        if (messageUser.getReaded() == 0) {
            messageUserMapper.updateMsgStatus(id, 1, new Date(), currentUser.getId());
            baseMapper.updateReadCount(messageUser.getMessageId());
        }
        return new ResultAPI<>(200, "完成阅读", null);
    }

    @Override
    public IPage<TroopMessageRespVO> selectStuList(PageVO vo, Integer userId) {
        IPage<TroopMessageRespVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        IPage<TroopMessageRespVO> stuList = baseMapper.selectStuList(page, userId);
        return stuList;
    }

    private List<Integer> sendUserList(MessageSendReqVO vo) {
        LambdaQueryWrapper<User> userWrapper = null;
        if (vo.getReceiveUserIds().size() == 0) {
            userWrapper = new LambdaQueryWrapper<>();
            userWrapper.ne(User::getRole, 0)
                    .eq(User::getDeleted, 0)
                    .ne(User::getRole, 1);
            return userMapper.selectList(userWrapper).stream()
                    .map(item -> item.getId())
                    .collect(Collectors.toList());
        }
        return vo.getReceiveUserIds();
    }
}




